// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ResourceCmdletGroup.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Cmdlet.Builtins
{
    #region

    using System;
    using System.Collections.Generic;
    using System.Reflection;

    using Org.OpenTrader.Framework.DataModel;

    #endregion

    /// <summary>
    /// The execution context command group.
    /// </summary>
    public class ResourceCmdletGroup : CmdletGroup
    {
        #region Constants and Fields

        /// <summary>
        /// The commands.
        /// </summary>
        private readonly IDictionary<string, Cmdlet> commands;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="ResourceCmdletGroup"/> class.
        /// </summary>
        public ResourceCmdletGroup()
        {
            this.commands = new Dictionary<string, Cmdlet>();

            Cmdlet cmd;

            cmd = new ListCmdlet(this);
            this.commands[cmd.Name] = cmd;

            cmd = new RegisterCmdlet(this);
            this.commands[cmd.Name] = cmd;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets Cmdlets.
        /// </summary>
        public override IDictionary<string, Cmdlet> Cmdlets
        {
            get
            {
                return this.commands;
            }
        }

        /// <summary>
        /// Gets Help.
        /// </summary>
        public override string Help
        {
            get
            {
                return "This command group is used to handle resources";
            }
        }

        /// <summary>
        /// Gets Name.
        /// </summary>
        public override string Name
        {
            get
            {
                return "resource";
            }
        }

        #endregion

        /// <summary>
        /// The run command.
        /// </summary>
        public class ListCmdlet : Cmdlet
        {
            #region Constants and Fields

            /// <summary>
            /// The identity.
            /// </summary>
            private static readonly Identity Identity = Identity.Create(MethodBase.GetCurrentMethod());

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="ListCmdlet"/> class.
            /// </summary>
            /// <param name="cmdletGroup">
            /// The command group.
            /// </param>
            public ListCmdlet(CmdletGroup cmdletGroup)
                : base(cmdletGroup)
            {
            }

            #endregion

            #region Properties

            /// <summary>
            /// Gets Help.
            /// </summary>
            public override string Help
            {
                get
                {
                    return "list resources from database";
                }
            }

            /// <summary>
            /// Gets Name.
            /// </summary>
            public override string Name
            {
                get
                {
                    return "list";
                }
            }

            /// <summary>
            /// Gets Usage.
            /// </summary>
            public override string Usage
            {
                get
                {
                    return string.Format("{0} {1}", this.CmdletGroup.Name, this.Name);
                }
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The execute.
            /// </summary>
            /// <param name="args">
            /// The args.
            /// </param>
            /// <returns>
            /// </returns>
            public override Status Execute(string[] args)
            {
                var resources = DBResourceHelpers.GetAllResourcesInDirectory("\\");

                Console.WriteLine(string.Format("{1,-8} {0,-40} {1,-40} {2,-22}", "Ext", "Name", "Directory", "Date"));
                foreach (var v in resources.Keys)
                {
                    var u = resources[v];
                    Console.WriteLine(
                        string.Format("{1,-8} {0,-40} {1,-40} {2,-22}", u.Extension, u.Name, u.Directory, u.TimeStamp));
                }

                return Status.Success(Identity);
            }

            #endregion
        }

        /// <summary>
        /// The deploy command.
        /// </summary>
        public class RegisterCmdlet : Cmdlet
        {
            #region Constants and Fields

            /// <summary>
            /// The identity.
            /// </summary>
            private static readonly Identity Identity = Identity.Create(MethodBase.GetCurrentMethod());

            #endregion

            #region Constructors and Destructors

            /// <summary>
            /// Initializes a new instance of the <see cref="RegisterCmdlet"/> class.
            /// </summary>
            /// <param name="cmdletGroup">
            /// The command group.
            /// </param>
            public RegisterCmdlet(CmdletGroup cmdletGroup)
                : base(cmdletGroup)
            {
            }

            #endregion

            #region Properties

            /// <summary>
            /// Gets Help.
            /// </summary>
            public override string Help
            {
                get
                {
                    return "register resources to database";
                }
            }

            /// <summary>
            /// Gets Name.
            /// </summary>
            public override string Name
            {
                get
                {
                    return "register";
                }
            }

            /// <summary>
            /// Gets Usage.
            /// </summary>
            public override string Usage
            {
                get
                {
                    return string.Format(
                        "{0} {1} resource_dbpath resource_1 ... resource_n", this.CmdletGroup.Name, this.Name);
                }
            }

            #endregion

            #region Public Methods

            /// <summary>
            /// The execute.
            /// </summary>
            /// <param name="args">
            /// The args.
            /// </param>
            /// <returns>
            /// </returns>
            public override Status Execute(string[] args)
            {
                if (args.Length == 0)
                {
                    return Status.Failure(Identity, "parameter resource_dbpath missing");
                }

                // The given parameters (if any)
                var parametersCount = Math.Max(0, args.Length - 1);
                var parameters = new string[] { };
                if (parametersCount > 0)
                {
                    parameters = new string[parametersCount];
                    Array.Copy(args, 1, parameters, 0, parametersCount);
                }

                return DBResourceHelpers.Register(args[0], parameters);
            }

            #endregion
        }
    }
}